package com.oceansoft.core.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.oceansoft.core.common.util.PageUtil;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.dao.IPersonMgrDao;
import com.oceansoft.core.domain.SysUser;
@Repository
public class PersonMgrDaoImpl implements IPersonMgrDao {

	@Resource
	private JdbcTemplate jdbcTemplate;
	@Override
	public Pagination<SysUser> findByPage(int pageIndex, SysUser sysUser) {
		StringBuffer sql = new StringBuffer("select dbid,name,policenum,dutyname from t_sys_user ");
		
		String count_sql = PageUtil.getCountSQL(sql.toString());
		int _total = jdbcTemplate.queryForInt(count_sql);
		
		
		String page_sql = PageUtil.getPageSQL(sql.toString(), pageIndex);
		List<SysUser> list = (List<SysUser>) jdbcTemplate.query(page_sql,
				new RowMapper<SysUser>() {
			@Override
			public SysUser mapRow(ResultSet rs, int arg1) throws SQLException {
				SysUser user = new SysUser();
				user.setName(rs.getString("NAME"));
				user.setDbId(rs.getLong("DBID"));
				user.setPoliceNum(rs.getString("POLICENUM"));
				user.setDutyName(rs.getString("dutyname"));
		
				return user;
			}});
		System.out.println(sql);
		return new Pagination<SysUser>(list, _total, pageIndex);
	}
	
	@Override
	public SysUser findByDbId(long dbid) {
	
		String sql = "select * from t_sys_user where dbid="+dbid;
		
		SysUser sysUser =  jdbcTemplate.queryForObject(sql, SysUser.class);
		
	
		
		return sysUser;
	}

	@Override
	public int deleteUserByDbId(long dbId) {
		String sql="delete from t_sys_user where dbId="+dbId;
		 jdbcTemplate.execute(sql);
		
		return 1;
	}

	
}
